// 云函数入口文件
const cloud = require('wx-server-sdk');
cloud.init();
const db = cloud.database();

exports.main = async (event, context) => {
  const { action, openid, moodId, timestamp } = event;
  if (action === 'saveMood') {
    if (!openid || !moodId || !timestamp) {
      return { success: false, error: '参数缺失' };
    }
    try {
      await db.collection('mood_info').add({
        data: {
          openid,
          moodId,
          timestamp: new Date(timestamp),
        },
      });
      return { success: true };
    } catch (err) {
      return { success: false, error: err.message };
    }
  }
  return { success: false, error: '无效操作' };
};
